#include <stdio.h> 
#include <sys/socket.h> 
#include <sys/time.h>
#include <linux/un.h> 
#include <string.h> 
#include <unistd.h> 
#include "role.h" 
#include <stdlib.h>
#include <string>
#include "test_mgr.h" 

#define SOCKET_PATH "/tmp/test_socket"
#define MAX_BACKLOG 512
#define SEND_INTERVAL 1

static long get_ms_time() {
    struct timeval tv;
    memset(&tv, 0x00, sizeof(tv));

    gettimeofday(&tv, nullptr);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

int main(int argc, char **argv) {
    int msg_total = 100000;
    struct sockaddr_un addr;
    int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

    if (sockfd < 0) {
        perror("create client socket error");
        return -1;
    }

    memset(&addr, 0x00, sizeof(addr));
    addr.sun_family = AF_UNIX;
    snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCKET_PATH);

    int ret = connect(sockfd, (const sockaddr*)&addr, sizeof(addr));
    long send_start = get_ms_time();

    if (ret < 0) {
        perror("connect to server error");
        return -1;
    }

    for (int i = 0; i < msg_total; ++i) {
        ret = write(sockfd, "echo", strlen("echo"));
        if (ret < 0) {
            perror("write error\n");
            break;
        }
    }

    double send_freq = (1000.0 * msg_total) / (get_ms_time() - send_start);

    printf("msg total:%d send_freq:%lf\n", msg_total, send_freq);

    return 0;
}
